import { defineStore } from 'pinia'

export const useMenuStore = defineStore('menu', {
    state:()=>({
        isCollapse: false,
        tagList: [],
        breadcrumbList: [],
        menuData: [
        {
            path: 'goods',
            component: () => import("@/views/GoodsView.vue"),
            name: 'goods',
            label: '商品管理',
            hidden: false,
            icon: 'Goods',
        },
        {
            path: 'user',
            component: () => import('@/views/UserView.vue'),
            name: 'user',
            label: '用户管理',
            hidden: false,
            icon: 'User',
        },
        {
            path: 'comment',
            component: () => import('@/views/CommentManage.vue'),
            name: 'comment',
            label: '评论管理',
            hidden: false,
            icon: 'ChatLineRound',
        },
        {
            path: 'order',
            component: () => import('@/views/OrderView.vue'),
            name: 'order',
            label: '订单管理',
            hidden: false,
            icon: 'ShoppingCartFull',
        },
        {
            path: 'data',
            component: () => import('@/views/DataView.vue'),
            name: 'data',
            label: '数据分析',
            hidden: false,
            icon: 'DataLine',
        },
        {
            path: 'promotion',
            component: () => import('@/views/Promotion.vue'),
            name: 'promotion',
            label: '促销活动管理',
            hidden: false,
            icon: 'ShoppingCartFull',
        }],
    }),
    getter:{
        Boolean:(state)=>state.isCollapse,
        Array:(state)=>state.menuData,
        Array: (state) => state.tabList,
        Array: (state) => state.breadcrumbList
    },
    actions: {
        setMenuData(data){
            this.menuData=data;
        },
        collapseMenu() {
            this.isCollapse = !this.isCollapse
          },
        updateBreadcrumb(value) {
            if (value.name !== 'shop') {
                var index = this.breadcrumbList.findIndex(item => item.name === value.name)
            if (index === -1) {
                this.breadcrumbList.push(value)
            } else {
                this.breadcrumbList.splice(index + 1)
            }
            } else {
                this.breadcrumbList.splice(1)
            }
        },
        updateTag(value) {
                if (value.name !== 'shop') {
                    var index = this.tagList.findIndex(item => item.name === value.name)
                if (index === -1) {
                    this.tagList.push(value)
                }
            }
        },
        // 设置tabList {title:'首页',path:'/index'}
        setTabList(data) {
            this.tabList.push(data)
        },
        // 删除tabList
        delTabList(value) {
        // 一定要重新赋值
                this.tabList = this.tabList.filter(item => {
                    if(item.path == value) {
                        return false
                    }else {
                        return true;
                }
            }
        )},
        // 设置activeTab
        setActive(value) {
            this.activeTab = value;
        },
        // 设置tabList {title:'商店管理',path:'/admin/shop'}
        setTabList(data) {
            this.tabList.push(data);
        },
        // 删除tabList
        delTabList(name) {
            // 一定要重新赋值
            this.tabList = this.tabList.filter(item => {
                if(item.path == name) {
                return false
                }else {
                    return true;
                }
            })
        },
        //tag删除时更新tagList数据
        closeTag(state, value) {
            var index = state.tagList.findIndex(item => item.name === value.name)
            state.tagList.splice(index, 1)
        },
        //重置菜单、面包屑、标签页
        reset() { // 正确的方法定义
            this.isCollapse = false;
            this.tagList = [];
            this.breadcrumbList = [];
            //this.menuData = [];
          }
    },
})